package tongwentongshu.com.app.utils;


import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

/**
 * http://blog.csdn.net/u013177564/article/details/49642173
 * 在listView,GridView使用glide加载图片的时候,会发生意想不到的图片变形,在源代码中终于找到了一种解决方法
 */
public class MyBitmapImageViewTarget extends BitmapImageViewTarget {
    public MyBitmapImageViewTarget(ImageView view) {
        super(view);
    }


    @Override public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
        if (bitmap != null && view.getScaleType() != ScaleType.FIT_XY) {
            view.setScaleType(ScaleType.FIT_XY);
        }
        super.onResourceReady(bitmap, anim);
    }


    @Override protected void setResource(Bitmap resource) {
        super.setResource(resource);
    }


    @Override public void onLoadFailed(Exception e, Drawable errorDrawable) {
        if (errorDrawable != null && view != null &&
                view.getScaleType() != ScaleType.CENTER_CROP) {
            view.setScaleType(ScaleType.CENTER_CROP);
        }
        super.onLoadFailed(e, errorDrawable);
    }


    @Override public void onLoadStarted(Drawable placeholder) {
        if (placeholder != null && view != null &&
                view.getScaleType() != ScaleType.CENTER_CROP) {
            view.setScaleType(ScaleType.CENTER_CROP);
        }
        super.onLoadStarted(placeholder);
    }


    @Override public void onLoadCleared(Drawable placeholder) {
        if (placeholder != null && placeholder != null && view != null &&
                view.getScaleType() != ScaleType.CENTER_CROP) {
            view.setScaleType(ScaleType.CENTER_CROP);
        }
        super.onLoadCleared(placeholder);
    }
}
